package jco3.JCoDestination.demo3;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import com.sap.conn.jco.ext.DestinationDataEventListener;
import com.sap.conn.jco.ext.DestinationDataProvider;

public class FileDestinationDataProviderImp implements DestinationDataProvider {

	private File dir;
	private String destName;
	private String suffix;
	
	public void setDestinationFile(File dir,String desrName,String suffix) {
		this.destName = desrName;
		this.dir = dir;
		this.suffix = suffix;
	}
	
	private Properties loadProperties(File dir,String destName,String suffix) {
		Properties props = null;
		File destFile = new File(dir, destName+"."+suffix);
		if(destFile.exists()) {
			props = new Properties();
			try {
				FileInputStream fInputStream = new FileInputStream(destFile);
				props.load(fInputStream);//fInputStream为读取到的文件，把xxx=yyy。(load)封装成Properties对象
				fInputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else {
			throw new RuntimeException("Destination file does not exist.");
		}
		return props;
	}
	
	@Override
	public Properties getDestinationProperties(String destName) {
		Properties props = null;
		props = this.loadProperties(this.dir, this.destName, this.suffix);
		return props;
	}

	@Override
	public void setDestinationDataEventListener(DestinationDataEventListener listener) {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean supportsEvents() {
		return false;
	}

}
